package pb

import (
	"chess_platform/internal/alisdk"
	. "chess_platform/internal/common"
	"chess_platform/models/pb"
	"chess_platform/models/pc"
	"fmt"
	"os"
)

//[pc]帐户管理
type UserController struct {
	GrantController
}

func (b *UserController) URLMapping() {
	//user:帐户列表
	b.Mapping("Card", b.Card)
	b.Mapping("UpdateUser", b.UpdateUser)
	b.Mapping("UserInfo", b.UserInfo)
	b.Mapping("Logout", b.Logout)
}

// @Title 合伙人招募总代的二维码
// @Description 合伙人招募总代的二维码
// @Success 0 {string} QR-code图片文件路径
// @Failure 400
// @Failure 404
// @router /card [post]
func (b *UserController) Card() {
	user, err := pb.UserInfoById(b.Uid)
	if err != nil {
		LogPB.Error(b.DatabaseError(err.Error()))
		return
	}
	ossPath := ""
	if user.Uid != 0 && user.NameUrl != "" {
		ossPath = user.NameUrl
	} else {
		//ossPath, err = pb.MakePbQRCode(b.Uid, "http://xshu.oss-cn-shenzhen.aliyuncs.com/platform/production/default_icon.jpg")
		//TODO:先生成一个没带任何文字的二维码
		ossPath, err = pb.MakePbQRCode(b.Uid, "")
		if err != nil {
			LogPF.Error(b.DatabaseError(err.Error()))
			return
		}
		qrFile := fmt.Sprintf("name-%v.png", b.Uid)
		os.Remove(qrFile)
		weChatFile := fmt.Sprintf("wechat-%v-%v.jpg", "word", b.Uid)
		os.Remove(weChatFile)
		file := fmt.Sprintf("new-name-%v-%v.png", "word", b.Uid)
		os.Remove(file)
	}
	b.Res.Data = alisdk.GetOssPathUrl(OssDomain) + "/" + ossPath
	b.ResponseSuccess()
}

// @Title 更新合伙人用户
// @Description 更新合伙人用户
// @Param   old_password 	body    string  true      "旧密码"
// @Param   new_password 	body    string  true      "新密码"
// @Success 0 {string} 状态码
// @Failure 399
// @router /user/update-user [post]
func (b *UserController) UpdateUser() {
	type Params struct {
		NewPassword string `json:"new_password"`
		OldPassword string `json:"old_password"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}

	b.Valid.Required(p.OldPassword, "old_password").Message("密码不能为空")
	b.Valid.MinSize(p.NewPassword, 6, "new_password").Message("密码长度为6-15个字符")
	b.Valid.MaxSize(p.NewPassword, 15, "new_password").Message("密码长度为6-15个字符")
	if !b.VerifyParamsError() {
		return
	}
	u, err := pc.PbUserInfoById(b.Uid)
	if err != nil {
		LogPB.Error(b.DatabaseError(err.Error()))
		return
	}
	if u.Uid != b.Uid {
		b.ResponseFail("用户不存在")
		return
	}
	if Md5sum(p.OldPassword) != u.Password {
		b.Res.Code = ErrUserPassword
		b.Res.Msg = MsgErrUserPassword
		b.Response()
		return
	}
	user := map[string]interface{}{
		"password": Md5sum(p.NewPassword),
	}
	_, err = pc.UpdatePbUser(user, b.Uid)
	if err != nil {
		LogPB.Error(b.DatabaseError(err.Error()))
		return
	}
	b.ResponseSuccess()
}

// @Title 合伙人用户详情
// @Description 合伙人用户详情
// @Param   uid  body    int     true      "用户uid"
// @Success 0 {object} common.PbUser 用户信息
// @router /user/user-info [post]
func (b *UserController) UserInfo() {
	user, err := pc.PbUserInfoById(b.Uid)
	if err != nil {
		LogPB.Error(b.DatabaseError(err.Error()))
		return
	}
	b.Res.Data = user
	b.ResponseSuccess()
}

// @Title 合伙人登出
// @Description 合伙人登出
// @Success 0 {string} 状态码
// @router /user/logout [post]
func (b *UserController) Logout() {
	key := fmt.Sprintf("%v%v", KeyPbTokenCache, b.Uid)
	err := RedisCache.Delete(key)
	if err != nil {
		LogPB.Error(b.SystemError(err.Error()))
		return
	}
	b.ResponseSuccess()
}
